package com.telesound.dataimport;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.telesound.dataimport.util.BeanUtil;
/**
 * 数据导入表规则的封装类
 * @author zhangchaofeng
 * @version 1.0
 * @date Sep 23, 2011
 */
public class DataImportTableRule{
	private String id;                //唯一标识，uuid
	private Date createTime;          //创建时间
	private String ruleName;          //规则名称
	private String endRow;            //结束行，空行或第N行
	private int headerIndex;          //标题行索引
	private int dataIndex=2;          //数据开始行索引
	private String tableName;         //表名称
	private boolean deleteRepeate;    //是否删除重复行
	private boolean enable;           //有效标识，是否已删除
	private String comments;          //备注
	private String templateFileLink;  //模板文件链接 
	private String blank1;
	private String blank2;
	private String blank3;
	private String blank4;
	private String blank5;
	private Map<String,DataImportColumnRule> columnRules=new HashMap<String,DataImportColumnRule>(); //包含的列规则集合

	@Override
	public String toString() {
		return "[id->"+id+",createTime->"+createTime+",ruleName->"+ruleName+",endRow->"
		+endRow+",headerIndex->"+headerIndex+",dataIndex->"+dataIndex+",tableName->"
		+tableName+",deleteRepeate->"+deleteRepeate+",enable->"+enable+"]";
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getEndRow() {
		return endRow;
	}

	public void setEndRow(String endRow) {
		this.endRow = endRow;
	}

	public int getHeaderIndex() {
		return headerIndex;
	}

	public void setHeaderIndex(int headerIndex) {
		this.headerIndex = headerIndex;
	}

	public int getDataIndex() {
		return dataIndex;
	}

	public void setDataIndex(int dataIndex) {
		this.dataIndex = dataIndex;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public boolean getDeleteRepeate() {
		return deleteRepeate;
	}

	public void setDeleteRepeate(boolean deleteRepeate) {
		this.deleteRepeate = deleteRepeate;
	}

	public Map<String,DataImportColumnRule> getColumnRules() {
		return columnRules;
	}

	public void addColumnRule(DataImportColumnRule columnRule){
		if(columnRules.containsKey(columnRule.getHeaderName())){
			return;
		}
		this.columnRules.put(columnRule.getHeaderName(), columnRule);
	}	
	
	public DataImportColumnRule removeColumnRule(String headerName){
		return this.columnRules.remove(headerName);
	}
	
	public DataImportColumnRule getColumnRule(String headerName){
		return this.columnRules.get(headerName);
	}
	
	public void updateColumnRule(DataImportColumnRule columnRule){
		this.columnRules.put(columnRule.getHeaderName(), columnRule);
	}

	public String getBlank1() {
		return blank1;
	}

	public void setBlank1(String blank1) {
		this.blank1 = blank1;
	}

	public String getBlank2() {
		return blank2;
	}

	public void setBlank2(String blank2) {
		this.blank2 = blank2;
	}

	public String getBlank3() {
		return blank3;
	}

	public void setBlank3(String blank3) {
		this.blank3 = blank3;
	}

	public String getBlank4() {
		return blank4;
	}

	public void setBlank4(String blank4) {
		this.blank4 = blank4;
	}

	public String getBlank5() {
		return blank5;
	}

	public void setBlank5(String blank5) {
		this.blank5 = blank5;
	}

	public boolean getEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	
	public static DataImportTableRule getTableRule(String tableRuleId){
		List args=new ArrayList();
		args.add(tableRuleId);
		String sql="select * from data_import_table_rule where id=?";
		Map ruleInfo=DBAccess.getSingleMap(sql, args);
		if(ruleInfo==null||ruleInfo.size()==0){
			return null;
		}
		DataImportTableRule rule=new DataImportTableRule();
		rule.setId(tableRuleId);
		rule=(DataImportTableRule)BeanUtil.setProperties(rule, ruleInfo);
		rule.setColumnRules(DataImportColumnRule.getColumnRuleMap(tableRuleId));
		return rule;
	}
	
	public static List getAllTableRules(){
		List result=new ArrayList();
		String sql="select * from data_import_table_rule where enable=1 order by create_time asc";
		List tableRules=DBAccess.getManyMap(sql, null);
		for(int i=0;i<tableRules.size();i++){
			DataImportTableRule rule=new DataImportTableRule();
			rule=(DataImportTableRule)BeanUtil.setProperties(rule,(Map)tableRules.get(i));
			result.add(rule);
		}
		return result;
	}

	public void setColumnRules(Map<String, DataImportColumnRule> columnRules) {
		this.columnRules = columnRules;
	}
	
	public static void main(String[] args) {
		/*DataImportTableRule rule=DataImportTableRule.getTableRule("4");
		System.out.println(rule.getTableName());
		System.out.println(rule.getHeaderIndex());
		System.out.println(rule.isDeleteRepeate());
		System.out.println(rule.createTime);*/
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public String getTemplateFileLink() {
		return templateFileLink;
	}

	public void setTemplateFileLink(String templateFileLink) {
		this.templateFileLink = templateFileLink;
	}
}
